package com.codeblue.tools.codeGenerator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.IOUtils;

public class Main {

	public  String packag(String line){
		return "'"+line+"'";
	}
	public static void main(String[] args) throws FileNotFoundException,
			IOException {
//		readContentFromGet();
//		String x = IOUtils.toString(new FileInputStream(new File("c:\\address.txt")));
		//System.out.println(x);
		
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\22.txt"),"gbk"));

//		System.out.println(" ============================= ");
//
//		System.out.println(" Contents of get request ");
//
//		System.out.println(" ============================= ");
		String sql ="insert into community_info(COMMUNITY_NAME,communityadd,LONGITUDE,LATITUDE,RADIUS,ISROUND,ISDELETE,CREATEUSER)values(" ;
		String sqlEnd = "1,1,'0','SYSTEM_ADMIN');";
		List<String> sqlList = new ArrayList<String>(760);
		Set<String> sqlset = new HashSet<String>();
		Main f= new Main();
		String lines;
		String total ="";
		int count=0;
		String recode="";
		while ((lines = reader.readLine()) != null) {
			count ++;
			if(count == 1){
				recode = sql;
				recode+= f.packag(lines)+",";
			}
			if(count==2){
				recode+= f.packag(lines.replace("地址：", "").trim())+",";
			}
			if(count==3){
				String pp = lines.replace("坐标：", "");
				String points[] = pp.split(",");
				recode = recode+ f.packag(points[0])+",";  //x
				recode = recode+ f.packag(points[1])+",";  //y
				recode = recode + sqlEnd;
				sqlList.add(recode);
				sqlset.add(recode);
				count=0;
			}
//			if(!StringUtils.startsWith(lines, "电话：")){
//				if(StringUtils.startsWith(lines, "地址：")){
//					lines= lines.replace("地址：", "");
//					recode = recode+ lines+",";
//				}else if(StringUtils.startsWith(lines, "坐标：")){
//					lines= lines.replace("坐标：", "");
//					String points[] = lines.split(",");
//					recode = recode+ points[0]+",";  //x
//					recode = recode+ points[1]+",";  //y
//					recode = recode + sqlEnd;
//					sqlList.add(recode);
//					recode="";
//				}else{
//					recode = sql;
//				}
//			}
//			
			
		}
		System.out.println(sqlList.size());
		System.out.println(sqlset.size());
		String tot="";
		for (String string : sqlset) {
//			System.out.println(string);
			tot+=string+"\n";
		}
		
		IOUtils.write(tot, new FileOutputStream(new File("c:\\out1.txt")));
		
	}

//	public static final String GET_URL = " http://api.map.baidu.com/?qt=s&c=75&wd=%E5%B0%8F%E5%8C%BA&rn=10&ie=utf-8&oue=1&res=api&callback=BMap._rd._cbk34148";
//
//	public static final String POST_URL = " http://localhost:8080/demo/  ";
//
//	public static void readContentFromGet() throws IOException {
//
//		// 拼凑get请求的URL字串，使用URLEncoder.encode对特殊和不可见字符进行编码
//
//		String getURL = GET_URL;
//
//		URL getUrl = new URL(getURL);
//
//		// 根据拼凑的URL，打开连接，URL.openConnection()函数会根据
//		// URL的类型，返回不同的URLConnection子类的对象，在这里我们的URL是一个http，因此它实际上返回的是HttpURLConnection
//
//		HttpURLConnection connection = (HttpURLConnection) getUrl
//				.openConnection();
//
//		// 建立与服务器的连接，并未发送数据
//
//		connection.connect();
//
//		// 发送数据到服务器并使用Reader读取返回的数据
//
//		BufferedReader reader = new BufferedReader(new InputStreamReader(
//				connection.getInputStream()));
//
////		System.out.println(" ============================= ");
////
////		System.out.println(" Contents of get request ");
////
////		System.out.println(" ============================= ");
//
//		String lines;
//		String total ="";
//		while ((lines = reader.readLine()) != null) {
//			total+= lines;
////			System.out.println(lines);
//
//		}
//
//		reader.close();
//
//		// 断开连接
//		
//		connection.disconnect();
//		String temp = total.replace("BMap._rd._cbk34148 && BMap._rd._cbk34148(", "");
//		temp = temp.substring(0, temp.length()-1);
////		System.out.println(temp);
//		
//		JSONObject jsonObj = JSONObject.parseObject(temp);
//		System.out.println(jsonObj);
//		
////JSONObject jsonObj = JSONObject.fromObject(json);//
////		System.out.println(" ============================= ");
////
////		System.out.println(" Contents of get request ends ");
////
////		System.out.println(" ============================= ");
//	}

}
